package m.jclecai.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import m.jclecai.com.R;

import java.text.DecimalFormat;
import java.util.List;

import m.jclecai.com.bean.ZqSPFItem;
import m.jclecai.com.listener.NotifyTextViewSetChanged;
import m.jclecai.com.utils.Utils;
import m.jclecai.com.utils.http.FastOk;

/**
 * 胜平负
 * Created by hp on 2017/3/21.
 */

public class ExpandSPFAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> groups;

    List<List<ZqSPFItem>> lists;
    NotifyTextViewSetChanged changed;

    public ExpandSPFAdapter(){

    }
    public ExpandSPFAdapter(Context context, List<String> groups, List<List<ZqSPFItem>> lists, NotifyTextViewSetChanged changed ){
        this.context=context;
        this.groups=groups;
        this.lists=lists;
        this.changed=changed;
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return lists.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return lists.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.item_parent,null);
            groupViewHolder=new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.list_item_parent_title.setText(groups.get(groupPosition));

        if(isExpanded){
            groupViewHolder.list_item_parent_icon.setImageResource(R.drawable.cz);
        }else {
            groupViewHolder.list_item_parent_icon.setImageResource(R.drawable.d0);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChlidViewHolder chlidViewHolder;
        if(convertView==null){
            convertView=View.inflate(context,R.layout.item_zq_spf,null);
            chlidViewHolder=new ChlidViewHolder(convertView);
            convertView.setTag(chlidViewHolder);
        }else {
            chlidViewHolder= (ChlidViewHolder) convertView.getTag();
        }
        final ZqSPFItem item = (ZqSPFItem) getChild(groupPosition,childPosition);
        String[] codes = item.getCodes();
        final List<String[]> rqsflists = FastOk.getList(codes[16], String[].class);
        DecimalFormat df = new DecimalFormat("#.00");

        chlidViewHolder.away_team_name.setText(Html.fromHtml(codes[4]));
        chlidViewHolder.home_team_name.setText(Html.fromHtml(codes[3]));
        chlidViewHolder.item_name.setText(codes[2]);//世界杯预选赛
        chlidViewHolder.item_time.setText(codes[7].substring(5, 16)+"\n"+codes[1]);

        chlidViewHolder.odd_home.setText("主胜"+df.format(Double.parseDouble(rqsflists.get(0)[0])));
        chlidViewHolder.odd_away.setText("平"+df.format(Double.parseDouble(rqsflists.get(0)[1])));
        chlidViewHolder.odd_draw.setText("主负"+df.format(Double.parseDouble(rqsflists.get(0)[2])));

        chlidViewHolder.odd_home_rang.setText("让胜"+df.format(Double.parseDouble(rqsflists.get(1)[0])));
        chlidViewHolder.odd_away_rang.setText("让平"+df.format(Double.parseDouble(rqsflists.get(1)[1])));
        chlidViewHolder.odd_draw_rang.setText("让负"+df.format(Double.parseDouble(rqsflists.get(1)[2])));
        chlidViewHolder.rang_num.setText(codes[5]);

        if (item.isOdd_home_select()) {
            chlidViewHolder.odd_home.setTextColor(Color.WHITE);
            chlidViewHolder.odd_home.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
        } else {
            chlidViewHolder.odd_home.setTextColor(Color.BLACK);
            chlidViewHolder.odd_home.setBackgroundColor(Color.WHITE);
        }

        if (item.isOdd_away_select()) {
            chlidViewHolder.odd_away.setTextColor(Color.WHITE);
            chlidViewHolder.odd_away.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
        } else {
            chlidViewHolder.odd_away.setTextColor(Color.BLACK);
            chlidViewHolder.odd_away.setBackgroundColor(Color.WHITE);
        }

        if (item.isOdd_draw_select()) {
            chlidViewHolder.odd_draw.setTextColor(Color.WHITE);
            chlidViewHolder.odd_draw.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
        } else {
            chlidViewHolder.odd_draw.setTextColor(Color.BLACK);
            chlidViewHolder.odd_draw.setBackgroundColor(Color.WHITE);
        }

        if (item.isOdd_home_r_select()) {
            chlidViewHolder.odd_home_rang.setTextColor(Color.WHITE);
            chlidViewHolder.odd_home_rang.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
        } else {
            chlidViewHolder.odd_home_rang.setTextColor(Color.BLACK);
            chlidViewHolder.odd_home_rang.setBackgroundColor(Color.WHITE);
        }

        if (item.isOdd_away_r_select()) {
            chlidViewHolder.odd_away_rang.setTextColor(Color.WHITE);
            chlidViewHolder.odd_away_rang.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
        } else {
            chlidViewHolder.odd_away_rang.setTextColor(Color.BLACK);
            chlidViewHolder.odd_away_rang.setBackgroundColor(Color.WHITE);
        }

        if (item.isOdd_draw_r_select()) {
            chlidViewHolder.odd_draw_rang.setTextColor(Color.WHITE);
            chlidViewHolder.odd_draw_rang.setBackgroundColor(android.graphics.Color.parseColor("#FF0026"));
        } else {
            chlidViewHolder.odd_draw_rang.setTextColor(Color.BLACK);
            chlidViewHolder.odd_draw_rang.setBackgroundColor(Color.WHITE);
        }
        chlidViewHolder.odd_home.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (item.isOdd_home_select()) {
                    item.setOdd_home_select(false);
                    item.getOdd_select().remove("3");
                    item.getOdd_sp().remove(rqsflists.get(0)[0]);
                } else {
                    item.setOdd_home_select(true);
                    item.getOdd_select().add("3");
                    item.getOdd_sp().add(rqsflists.get(0)[0]);
                }
                addItem(item);
            }
        });

        chlidViewHolder.odd_away.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (item.isOdd_away_select()) {
                    item.setOdd_away_select(false);
                    item.getOdd_select().remove("1");
                    item.getOdd_sp().remove(rqsflists.get(0)[1]);
                } else {
                    item.setOdd_away_select(true);
                    item.getOdd_select().add("1");
                    item.getOdd_sp().add(rqsflists.get(0)[1]);
                }
                addItem(item);
            }
        });
        chlidViewHolder.odd_draw.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (item.isOdd_draw_select()) {
                    item.setOdd_draw_select(false);
                    item.getOdd_select().remove("0");
                    item.getOdd_sp().remove(rqsflists.get(0)[2]);
                } else {
                    item.setOdd_draw_select(true);
                    item.getOdd_select().add("0");
                    item.getOdd_sp().add(rqsflists.get(0)[2]);
                }
                addItem(item);
            }
        });

        chlidViewHolder.odd_home_rang.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (item.isOdd_home_r_select()) {
                    item.setOdd_home_r_select(false);
                    item.getOdd_select_r().remove("3");
                    item.getOdd_rsp().remove(rqsflists.get(1)[0]);
                } else {
                    item.setOdd_home_r_select(true);
                    item.getOdd_select_r().add("3");
                    item.getOdd_rsp().add(rqsflists.get(1)[0]);
                }
                addItem(item);
            }
        });
        chlidViewHolder.odd_away_rang.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (item.isOdd_away_r_select()) {
                    item.setOdd_away_r_select(false);
                    item.getOdd_select_r().remove("1");
                    item.getOdd_rsp().remove(rqsflists.get(1)[1]);
                } else {
                    item.setOdd_away_r_select(true);
                    item.getOdd_select_r().add("1");
                    item.getOdd_rsp().add(rqsflists.get(1)[1]);
                }
                addItem(item);
            }
        });
        chlidViewHolder.odd_draw_rang.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (item.isOdd_draw_r_select()) {
                    item.setOdd_draw_r_select(false);
                    item.getOdd_select_r().remove("0");
                    item.getOdd_rsp().remove(rqsflists.get(1)[2]);
                } else {
                    item.setOdd_draw_r_select(true);
                    item.getOdd_select_r().add("0");
                    item.getOdd_rsp().add(rqsflists.get(1)[2]);
                }
                addItem(item);
            }
        });
        return convertView;
    }

    private void addItem(ZqSPFItem item) {
        // TODO Auto-generated method stub
        if (item.isOdd_home_select() || item.isOdd_away_select() || item.isOdd_draw_select() || item.isOdd_away_r_select() || item.isOdd_draw_r_select() || item.isOdd_home_r_select()) {
            if (!Utils.ZQ_SPF_ITEM_LIST.contains(item)) {
                Utils.ZQ_SPF_ITEM_LIST.add(item);
            }
        } else {
            if (Utils.ZQ_SPF_ITEM_LIST.contains(item)) {
                Utils.ZQ_SPF_ITEM_LIST.remove(item);
            }
        }
        notifyDataSetChanged();
        changed.onClickSetChangedText();
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    static class GroupViewHolder{
        private final TextView list_item_parent_title;
        private final ImageView list_item_parent_icon;

        public GroupViewHolder(View view){
            list_item_parent_title = (TextView) view.findViewById(R.id.list_item_parent_title);
            list_item_parent_icon = (ImageView) view.findViewById(R.id.list_item_parent_icon);
        }
    }
    static class ChlidViewHolder{
        private final TextView item_name;
        private final TextView item_time;
        private final TextView home_team_name;
        private final TextView rang_num;
        private final TextView away_team_name;
        private final TextView odd_home;
        private final TextView odd_away;
        private final TextView odd_draw;
        private final TextView odd_home_rang;
        private final TextView odd_away_rang;
        private final TextView odd_draw_rang;

        public ChlidViewHolder(View view){
            item_name = (TextView) view.findViewById(R.id.item_name);
            item_time = (TextView) view.findViewById(R.id.item_time);
            home_team_name = (TextView) view.findViewById(R.id.home_team_name);
            rang_num = (TextView) view.findViewById(R.id.rang_num);
            away_team_name = (TextView) view.findViewById(R.id.away_team_name);
            odd_home = (TextView) view.findViewById(R.id.odd_home);
            odd_away = (TextView) view.findViewById(R.id.odd_away);
            odd_draw = (TextView) view.findViewById(R.id.odd_draw);
            odd_home_rang = (TextView) view.findViewById(R.id.odd_home_rang);
            odd_away_rang = (TextView) view.findViewById(R.id.odd_away_rang);
            odd_draw_rang = (TextView) view.findViewById(R.id.odd_draw_rang);
        }
    }
}
